use std::path::PathBuf;

use structopt::StructOpt;
use tracing::{info, error};

use crate::file::common::handle_lines;

/// 打印文件命令
#[derive(StructOpt)]
pub struct Cat {
    /// 文件路径
    #[structopt(parse(from_os_str))]
    path: PathBuf,

    /// 输出行数
    #[structopt(short = "c", long = "count")]
    count: bool,
}

pub fn handle_cat(cat: Cat) {
    let mut count = 0;
    let read_line = handle_lines(&cat.path, |line| {
        info!("{}", line);
        count += 1;
    });
    if let Err(e) = read_line {
        error!("cat failed: {}", e);
        return;
    }
    if count == 0 {
        info!("{}", "no content");
    }
    if cat.count {
        info!("total line count: {}", count);
    }
}
